{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# {class}`~tvm.target.VirtualDevice`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "import tvm.testing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 通过 `tvm.device(\"cuda\")` 创建物理设备\n",
    "2. 构造 VirtualDevice 时仅传递物理设备参数\n",
    "3. 验证四个关键属性的默认值：\n",
    "   - 设备类型对应 CUDA（DLDeviceType 枚举值 2）\n",
    "   - 虚拟设备 ID 初始化为 0\n",
    "   - 目标编译器为空（需后续显式设置）\n",
    "   - 内存作用域未指定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "virtual_device = tvm.target.VirtualDevice(tvm.device(\"cuda\"))\n",
    "# 断言设备类型为CUDA（kDLCUDA对应值2）\n",
    "assert virtual_device.device_type == 2\n",
    "# 虚拟设备ID默认为0\n",
    "assert virtual_device.virtual_device_id == 0\n",
    "# 未指定target时自动推导为None\n",
    "assert virtual_device.target is None  \n",
    "# 内存作用域默认为空字符串\n",
    "assert virtual_device.memory_scope == \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 创建CUDA编译目标对象\n",
    "2. 同时传入物理设备（CUDA）和编译目标构造VirtualDevice\n",
    "3. 验证三个关键属性：\n",
    "   - 设备类型匹配CUDA枚举值\n",
    "   - 目标编译器对象正确绑定\n",
    "   - 内存作用域保持未指定状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = tvm.target.Target(\"cuda\")\n",
    "virtual_device = tvm.target.VirtualDevice(tvm.device(\"cuda\"), target)\n",
    "assert virtual_device.device_type == 2  # ie kDLCUDA\n",
    "assert virtual_device.target == target\n",
    "assert virtual_device.memory_scope == \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "验证当同时指定物理设备、编译目标和内存作用域时，VirtualDevice对象的属性正确性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = tvm.target.Target(\"cuda\")\n",
    "scope = \"local\"\n",
    "virtual_device = tvm.target.VirtualDevice(tvm.device(\"cuda\"), target, scope)\n",
    "# 验证设备类型为CUDA（kDLCUDA=2）\n",
    "assert virtual_device.device_type == 2\n",
    "# 确认目标编译器绑定正确\n",
    "assert virtual_device.target == target\n",
    "# 检查内存作用域设置生效\n",
    "assert virtual_device.memory_scope == scope"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**核心验证点**：\n",
    "1. 完整参数构造：同时传入物理设备（CUDA）、编译目标（cuda Target）和内存作用域（local）\n",
    "2. 属性三元组验证：\n",
    "   - 设备类型映射到CUDA的DLDeviceType枚举值\n",
    "   - Target对象与传入参数严格相等\n",
    "   - 内存作用域字符串完全匹配输入参数\n",
    "3. 扩展能力测试：验证VirtualDevice对内存作用域参数的支持机制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "该测试验证了 TVM 中 VirtualDevice 对象在同时指定物理设备、编译目标和内存作用域时的正确构造。以下是关键分析点：\n",
    "\n",
    "1. **三元参数验证**：\n",
    "   物理设备（cuda）对应DLDeviceType枚举值2\n",
    "   编译目标（target）绑定CUDA编译配置\n",
    "   内存作用域（\"local\"）明确指定存储范围\n",
    "\n",
    "2. **设备映射机制**：\n",
    "   ```python\n",
    "   virtual_device = tvm.target.VirtualDevice(tvm.device(\"cuda\"), target, scope)\n",
    "   ```\n",
    "   通过device_type字段实现硬件抽象层（HAL）与DLPack标准的对接\n",
    "\n",
    "3. **内存作用域校验**：\n",
    "   验证memory_scope参数支持不同存储层级的配置：\n",
    "   - global：全局显存\n",
    "   - local：片上共享内存\n",
    "   - texture：纹理内存等特殊存储\n",
    "\n",
    "4. **目标编译器绑定**：\n",
    "   确保target参数正确传递CUDA编译配置（如compute capability、线程配置等）\n",
    "\n",
    "该测试覆盖了TVM运行时设备管理系统的核心功能，为后续异构计算调度提供基础设备抽象支持。\n",
    "\n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
